import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import org.json.JSONException;
import win.Win;

import java.io.File;
import java.io.IOException;


public class Controller {
    private PhotosManager photosManager;
    private Thread serviceThread;
    private Thread wallpaperThread;
    private Thread nextThread;

    public void timerStop() {
        if (null != serviceThread) serviceThread.stop();
        if (null != wallpaperThread) wallpaperThread.stop();
        if (null != nextThread) nextThread.stop();
    }

    public Controller() throws IOException, JSONException {
        photosManager = PhotosManager.getPhotosManager();
        photosManager.tmpLoading();

        serviceThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        photosManager.requirePhotos();
                        photosManager.DownloadRegularPhotos();
                        Thread.sleep(500);
                    } catch (Exception e) {
                    }
                }
            }
        });
        serviceThread.start();
    }

    @FXML
    private Button wallpaperBtn;
    @FXML
    private Button nextBtn;
    @FXML
    private ImageView imgView;
    @FXML
    private Label msgLabel;

    @FXML
    protected void OnWallpaperBtnClick(ActionEvent event) {
        wallpaperBtn.setDisable(true);
        nextBtn.setDisable(true);
        msgLabel.setText("正在下载壁纸");
        // TODO
        wallpaperThread = new Thread(new Task() {
            @Override
            protected Object call() {
                String imgPath;
                try {
                    imgPath = photosManager.DownloadCurrentPhoto();
                } catch (IOException e) {
                    return null;
                }
                if (null != imgPath) Win.changeWallpaper(imgPath);
                return null;
            }

            @Override
            protected void succeeded() {
                msgLabel.setText("");
                wallpaperBtn.setDisable(false);
                nextBtn.setDisable(false);
            }
        });
        wallpaperThread.start();
    }

    @FXML
    protected void OnNextBtnClick(ActionEvent event) {
        wallpaperBtn.setDisable(true);
        nextBtn.setDisable(true);
        msgLabel.setText("加载中");
        // TODO
        nextThread = new Thread(new Task() {
            File imgFile;

            @Override
            protected Object call() {
                try {
                    do {
                        imgFile = photosManager.next();
                    } while (!imgFile.exists());
                    Image image = new Image(imgFile.toURL().toString(), true);
                    imgView.setImage(image);
                } catch (Exception e) {
                }
                return null;
            }

            @Override
            protected void succeeded() {
                wallpaperBtn.setDisable(false);
                nextBtn.setDisable(false);
                msgLabel.setText("");
            }
        });
        nextThread.start();
    }
}
